package com.xungou.city.service.impl;

import com.xungou.city.dao.ICity;
import com.xungou.city.pojo.City;
import com.xungou.city.service.ICityService;
import com.xungou.core.mvc.AbstractService;
import com.xungou.core.mvc.IDao;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service("cityService")
public class CityServiceImpl extends AbstractService<City> implements ICityService {
    @Resource  
    private ICity iCity;

    /**
     *
     * @param record code值由当前方法生成
     * @param params parentNodeCode:父节点code值
     * @return
     */
    public int insert(City record, @RequestParam Map<String, String> params) {
        String code = iCity.getCode(record);
        if(code!=null){
            code = (Long.parseLong(code)+1)+"";
            while(code.length()<3)
                code = "0"+code;
            if(record.getLevel()>0)
                code = params.get("parentNodeCode")+code;
        }else{
            if(record.getLevel()>0)
                code = params.get("parentNodeCode")+"001";
            else
                code = "001";
        }
        record.setCode(code);
        return super.insert(record);
    }

    public int delete(Integer id) {
        City city = selectByPrimaryKey(id);
        return iCity.deleteByCode(city.getCode());
    }

    public List<Map<String, Object>> getCityTree() {
        return iCity.getCityTree();
    }

    @Override
    public List<Map<String, Object>> getCityList(Map<String, String> params) {
        return iCity.getCityList(params);
    }

    @Override
    protected IDao<City> getDao() {
        return iCity;
    }
}